package com.hzhc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.entity.CarInfo;
import com.hzhc.entity.CarTeamPlan;
import com.hzhc.entity.CompanyInfo;
import com.hzhc.entity.YwOrderCar;
import com.hzhc.mapper.*;
import com.hzhc.service.CarTeamPlanService;
import com.hzhc.service.YwOrderCarService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.StringUtils;
import com.hzhc.vo.CarTeamPlanVo;
import com.hzhc.vo.YwOrderVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 车辆计划 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-11-06
 */
@Service
@RequiredArgsConstructor
public class CarTeamPlanServiceImpl extends ServiceImpl<CarTeamPlanMapper, CarTeamPlan> implements CarTeamPlanService {
    private final YwOrderCarService ywOrderCarService;
    private final YwOrderCarMapper ywOrderCarMapper;
    private final YwOrderMapper ywOrderMapper;
    private final CarInfoMapper carInfoMapper;
    private final CompanyInfoMapper companyInfoMapper;

    @Override
    public List<CarTeamPlanVo> getCarNos(CarTeamPlanVo vo) {
        List<CarTeamPlanVo> list= baseMapper.getCarNos(vo);
        for (CarTeamPlanVo carTeamPlanVo : list) {
            List<String> list1 = Arrays.asList(carTeamPlanVo.getCarNo().split(","));
            List<CarInfo> carInfos = carInfoMapper.selectList(new QueryWrapper<CarInfo>().eq("disable", 0).in("car_no", list1));
            carTeamPlanVo.setCarList(carInfos);

            //获取审核过的车辆信息
            if(StringUtils.isNotEmpty(carTeamPlanVo.getCarNoAudit())){
                List<String> list2 = Arrays.asList(carTeamPlanVo.getCarNoAudit().split(","));
                List<CarInfo> carInfos1 = carInfoMapper.selectList(new QueryWrapper<CarInfo>().eq("disable", 0).in("car_no", list2));
                carTeamPlanVo.setCarAuditList(carInfos1);
            }

        }
        return list;
    }

    @Override
    public ResultUtil<T> updateCarTeamPlan(CarTeamPlanVo vo) {
        YwOrderVo ywOrderVo=new YwOrderVo();
        ywOrderVo.setCompanyId(vo.getCompanyId());
        ywOrderVo.setOrderType(vo.getOrderType());
//        List<YwOrderVo> powerOrderList = ywOrderMapper.getPowerOrderList1(ywOrderVo);
        if(vo.getId()!=null){
            CarTeamPlan carTeamPlan = baseMapper.selectById(vo.getId());
            if(vo.getCarList().size()>0){
                String collect = vo.getCarList().stream().map(CarInfo::getCarNo).collect(Collectors.joining(","));
                carTeamPlan.setCarNo(collect);
                baseMapper.updateById(carTeamPlan);
            }else{
                carTeamPlan.setCarNo("");
                baseMapper.updateById(carTeamPlan);
            }

//            //新增 删除
//            List<YwOrderCar> ywOrderCars=new ArrayList<>();
//            List<YwOrderCar> oldOrderCars=new ArrayList<>();
//            if("ZJ".equals(carTeamPlan.getOrderType())){
//                oldOrderCars=ywOrderCarMapper.selectList(new QueryWrapper<YwOrderCar>().eq("disable",0).eq("company_id",vo.getCompanyId())
//                        .and(wrapper -> wrapper.like("order_no","ZJ").or().like("order_no","KJ")));
//            }else{
//                oldOrderCars=ywOrderCarMapper.selectList(new QueryWrapper<YwOrderCar>().eq("disable",0).eq("company_id",vo.getCompanyId())
//                        .like("order_no",carTeamPlan.getOrderType()));
//            }
//
//            if(oldOrderCars.size()>0){
//                for (YwOrderCar oldOrderCar : oldOrderCars) {
//                    oldOrderCar.setDisable(1);
//                }
//            }
//            ywOrderCarService.updateBatchById(oldOrderCars);

//            if(powerOrderList.size()>0){
//                for (CarInfo carInfo : vo.getCarList()) {
//                    for (YwOrderVo orderVo : powerOrderList) {
//                        YwOrderCar ywOrderCar = new YwOrderCar();
//                        ywOrderCar.setOrderNo(orderVo.getOrderNo());
//                        ywOrderCar.setTruckNo(carInfo.getCarNo());
//                        ywOrderCar.setCompanyId(vo.getCompanyId());
//                        ywOrderCar.setCompanyName(vo.getCompanyName());
//                        ywOrderCars.add(ywOrderCar);
//                    }
//                }
//            }
//            if(ywOrderCars.size()>0){
//                ywOrderCarService.saveBatch(ywOrderCars);
//            }
        }else{
            //新增
            if(vo.getCarList().size()>0){
                String collect = vo.getCarList().stream().map(CarInfo::getCarNo).collect(Collectors.joining(","));
                CarTeamPlan carTeamPlan=new CarTeamPlan();
                carTeamPlan.setCompanyId(vo.getCompanyId());
                carTeamPlan.setCompanyName(vo.getRealName());
                carTeamPlan.setCarNo(collect);
                carTeamPlan.setOrderType(vo.getOrderType());
                carTeamPlan.setUserId(vo.getUserId());
                carTeamPlan.setCreateTime(LocalDateTime.now());
                baseMapper.insert(carTeamPlan);
            }
//            List<YwOrderCar> ywOrderCars=new ArrayList<>();
//            if(powerOrderList.size()>0){
//                for (CarInfo carInfo : vo.getCarList()) {
//                    for (YwOrderVo orderVo : powerOrderList) {
//                        YwOrderCar ywOrderCar = new YwOrderCar();
//                        ywOrderCar.setOrderNo(orderVo.getOrderNo());
//                        ywOrderCar.setTruckNo(carInfo.getCarNo());
//                        ywOrderCar.setCompanyId(vo.getCompanyId());
//                        ywOrderCar.setCompanyName(vo.getCompanyName());
//                        ywOrderCars.add(ywOrderCar);
//                    }
//                }
//            }
//            if(ywOrderCars.size()>0){
//                ywOrderCarService.saveBatch(ywOrderCars);
//            }
        }
        return ResultUtil.ok();
    }

    @Override
    public ResultUtil<T> auditCarTeamPlan(CarTeamPlanVo vo) {
        YwOrderVo ywOrderVo=new YwOrderVo();
        ywOrderVo.setCompanyId(vo.getCompanyId());
        ywOrderVo.setOrderType(vo.getOrderType());
        CompanyInfo companyInfo = companyInfoMapper.selectById(vo.getCompanyId());
        List<YwOrderVo> powerOrderList = ywOrderMapper.getPowerOrderList1(ywOrderVo);
        if(vo.getId()!=null){
            CarTeamPlan carTeamPlan = baseMapper.selectById(vo.getId());
            if(vo.getCarList().size()>0){
                String collect = vo.getCarList().stream().map(CarInfo::getCarNo).collect(Collectors.joining(","));
                carTeamPlan.setCarNoAudit(collect);
                baseMapper.updateById(carTeamPlan);
            }else{
                carTeamPlan.setCarNoAudit("");
                baseMapper.updateById(carTeamPlan);
            }

            //新增 删除
            List<YwOrderCar> ywOrderCars=new ArrayList<>();
            List<YwOrderCar> oldOrderCars=new ArrayList<>();
            if("ZJ".equals(carTeamPlan.getOrderType())){
                oldOrderCars=ywOrderCarMapper.selectList(new QueryWrapper<YwOrderCar>().eq("disable",0).eq("company_id",vo.getCompanyId())
                        .and(wrapper -> wrapper.like("order_no","ZJ").or().like("order_no","KJ")));
            }else{
                oldOrderCars=ywOrderCarMapper.selectList(new QueryWrapper<YwOrderCar>().eq("disable",0).eq("company_id",vo.getCompanyId())
                        .like("order_no",carTeamPlan.getOrderType()));
            }

            if(oldOrderCars.size()>0){
                for (YwOrderCar oldOrderCar : oldOrderCars) {
                    oldOrderCar.setDisable(1);
                }
            }
            ywOrderCarService.updateBatchById(oldOrderCars);

            if(powerOrderList.size()>0){
                for (CarInfo carInfo : vo.getCarList()) {
                    for (YwOrderVo orderVo : powerOrderList) {
                        YwOrderCar ywOrderCar = new YwOrderCar();
                        ywOrderCar.setOrderNo(orderVo.getOrderNo());
                        ywOrderCar.setTruckNo(carInfo.getCarNo());
                        ywOrderCar.setCompanyId(vo.getCompanyId());
                        ywOrderCar.setCompanyName(companyInfo.getName());
                        ywOrderCar.setAuditState(1);
                        ywOrderCars.add(ywOrderCar);
                    }
                }
            }
            if(ywOrderCars.size()>0){
                ywOrderCarService.saveBatch(ywOrderCars);
            }
        }else{
            //新增
            if(vo.getCarList().size()>0){
                String collect = vo.getCarList().stream().map(CarInfo::getCarNo).collect(Collectors.joining(","));
                CarTeamPlan carTeamPlan=new CarTeamPlan();
                carTeamPlan.setCompanyId(vo.getCompanyId());
                carTeamPlan.setCompanyName(vo.getRealName());
                carTeamPlan.setCarNoAudit(collect);
                carTeamPlan.setOrderType(vo.getOrderType());
                carTeamPlan.setUserId(vo.getUserId());
                carTeamPlan.setCreateTime(LocalDateTime.now());
                baseMapper.insert(carTeamPlan);
            }
            List<YwOrderCar> ywOrderCars=new ArrayList<>();
            if(powerOrderList.size()>0){
                for (CarInfo carInfo : vo.getCarList()) {
                    for (YwOrderVo orderVo : powerOrderList) {
                        YwOrderCar ywOrderCar = new YwOrderCar();
                        ywOrderCar.setOrderNo(orderVo.getOrderNo());
                        ywOrderCar.setTruckNo(carInfo.getCarNo());
                        ywOrderCar.setCompanyId(vo.getCompanyId());
                        ywOrderCar.setCompanyName(companyInfo.getName());
                        ywOrderCar.setAuditState(1);
                        ywOrderCars.add(ywOrderCar);
                    }
                }
            }
            if(ywOrderCars.size()>0){
                ywOrderCarService.saveBatch(ywOrderCars);
            }
        }
        return ResultUtil.ok();
    }
}
